package com.pengtu.gsj.service.impl.check.lzgl.enforSup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.lzgl.LawInformOpenDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.LawInformOpen;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.enforSup.EnforInfoDiscloService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.StringUtils;


@Service
@Transactional
public class EnforInfoDiscloServiceImpl implements EnforInfoDiscloService {
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private LawInformOpenDao lawInformOpenDao;
	
	@Override
	public List<LawInformOpen> getLawInformOpenList(QueryFilter queryFilter){
		return lawInformOpenDao.find(queryFilter, "testYear", true);
	}
	
	@Override
	public Map<String, Object> getDevelopPlanmMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<LawInformOpen> lawList = new ArrayList<LawInformOpen>();
		List<Object> value = new ArrayList<Object>();
		List<List<String>> resultLists = new ArrayList<List<String>>();
		List<String> checkResult = Arrays.asList("0");
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		String enInformId = (String)jsonObject.get("lawInformOpenId");
		//省级交通部门设置公示醒目，且信息全面得分
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//基层执法站所设置公示，且信息全面得分
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		//基层执法站所设置公示，但信息不全面得分
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		double total = yearFull; 					//检查得分
		double percentage = 0; 						//系统完善度
		double tableFull  = yearFull + poitMark*2;	//满分
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 1)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		
		boolean flag = false;
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isEmpty(enInformId)) {
			queryFilter.addFilter("organizationType", "2", MatchType.EQ);
			lawList = this.getLawInformOpenList(queryFilter);
		} else {
			value.add(enInformId);
			flag = true;
			String[] idArrs = enInformId.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				checkResult = Arrays.asList("0");
				LawInformOpen lawInformOpen = lawInformOpenDao.get(idArrs[i]);
				
				//1表示信息公布完整，2表示信息公布不完整
				if (!StringUtils.isEmpty(lawInformOpen.getPublishWay()) && "1".equals(lawInformOpen.getPublishFull())) {
					total += poitMark;
					checkResult.set(0, String.valueOf(poitMark));
				} else if (!StringUtils.isEmpty(lawInformOpen.getPublishWay()) && "2".equals(lawInformOpen.getPublishFull())) {
					total += reserveMark;
					checkResult.set(0, String.valueOf(reserveMark));
				}
				resultLists.add(checkResult);
				lawList.add(lawInformOpen);
			}
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,1));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, value);
		map.put("percent", percentage);
		map.put("years", years);
		map.put("att", list);
		map.put("flag", flag);
		map.put("yearFull", yearFull);
		map.put("lawList", lawList);
		map.put("resultLists", resultLists);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
